<?php
/* -----------------------------------------------------------------
 *                        Aveghe My Blog
 * -----------------------------------------------------------------
 * Aveghe My Blog: made your blog easy!
 * Copyright (C) 2008 Elia Contini
 * 
 * Aveghe My Blog is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * Aveghe My Blog is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 */

class Aveghe_View_Helper_CategoriesList
	{
		public function categoriesList($categories, $currentCategory)
			{
				$result = 'No Categories Found.';
				
				if(count($categories) > 0)
					{
						$result = "\r\t\t<ul id=\"categories\">\n";
						
						for($i = 0; $i < count($categories); $i++)
							{
								$category = $categories[$i];
								
								if($category['document_parent_directory'] == 0)
									{
										if($currentCategory == 0)
											$result .= "\t\t\t<li><a href=\"".BASE_URL."/post/by-category/id/".$category['document_id']."\" title=\"\">".stripcslashes($category['document_name']).'</a>';
										else 
											{
												if($category['document_id'] == $currentCategory)  // first level
													{
														if($category['document_id'] == $currentCategory)
															$result .= "\t\t\t<li><a id=\"currentCategory\" href=\"".BASE_URL."/post/by-category/id/".$category['document_id']."\" title=\"\">".stripcslashes($category['document_name']).'</a>';
														else
															$result .= "\t\t\t<li><a href=\"".BASE_URL."/post/by-category/id/".$category['document_id']."\" title=\"\">".stripcslashes($category['document_name']).'</a>';
														
														$num = 0;
														$subResult = "<ul>";
														for($j = $i; $j < count($categories); $j++)
															{
																$subCategory = $categories[$j];
																if($subCategory['document_parent_directory'] == $category['document_id'])
																	{
																		if($subCategory['document_id'] == $currentCategory)
																			$subResult .= '<li><a id=\"currentCategory\" href="'.BASE_URL.'/post/by-category/id/';
																		else
																			$subResult .= '<li><a href="'.BASE_URL.'/post/by-category/id/';
																		
																		$subResult .= $subCategory['document_id'].'" title="">'.stripcslashes($subCategory['document_name']);
																		$subResult .= "</a></li>\n";
																		$num++;
																	}
															}
														$subResult .= "</ul>";
														if($num > 0)
															$result .= $subResult;
													}
												else // second level
													{
														$result .= "\t\t\t<li><a href=\"".BASE_URL."/post/by-category/id/".$category['document_id']."\" title=\"\">".stripcslashes($category['document_name']).'</a>';			
														
														$num = 0;
														$found = false;
														$subResult = "\r\t\t\t<ul>\n";
														for($j = $i; $j < count($categories); $j++)
															{
																$subCategory = $categories[$j];
																if($subCategory['document_parent_directory'] == $category['document_id'])
																	{
																		if($subCategory['document_id'] == $currentCategory)
																			{
																				$subResult .= "\t\t\t\t<li><a id=\"currentCategory\" href=\"".BASE_URL.'/post/by-category/id/';
																				$found = true;
																			}
																		else
																			$subResult .= "\t\t\t\t<li><a href=\"".BASE_URL.'/post/by-category/id/';
																		
																		$subResult .= $subCategory['document_id'].'" title="">'.stripcslashes($subCategory['document_name']);
																		$subResult .= "</a></li>\n";
																		$num++;
																	}
															}
														$subResult .= "\t\t\t</ul>\n\t\t\t";
														if($num > 0 && $found)
															$result .= $subResult;
													}
											}
									    $result .= "</li>\n";
									}
							} // end for
								
						$result .= "\t\t</ul>\n";
					}
					
				return $result;
			}
	}
?>